package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 分页查询套餐
     *
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        long total = page.getTotal();
        List<SetmealVO> records = page.getResult();
        return new PageResult(total, records);
    }

    /**
     * 新增套餐
     *
     * @return
     */
    @Transactional
    public int save(SetmealDTO setmealDTO) {
        // 添加套餐主体
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        int row = setmealMapper.save(setmeal);// 初始默认停售
        // 添加套餐关联菜品到中间表
        Long id = setmeal.getId();
        List<SetmealDish> list = setmealDTO.getSetmealDishes();
        if (list != null && list.size() != 0) {
            list = list.stream().peek(item -> {
                item.setSetmealId(id);
            }).collect(Collectors.toList());
            setmealDishMapper.insertBatch(list);
        }
        return row;
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    public SetmealVO getVOById(Long id) {
        // 查询主体
        Setmeal setmeal = setmealMapper.getById(id);
        ArrayList<Long> list = new ArrayList<>();
        list.add(id);
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmeals(list);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     * @return
     */
    @Transactional
    public int update(SetmealDTO setmealDTO) {
        // 修改主体
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        int row = setmealMapper.update(setmeal);
        // 修改中间表(删除+新增)
        Long id = setmealDTO.getId();
        ArrayList<Long> list = new ArrayList<>();
        list.add(id);
        setmealDishMapper.deleteBatchBySetmeals(list);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() != 0) {
            // 添加
            setmealDishes = setmealDishes.stream().peek(item -> {
                item.setSetmealId(id);
            }).collect(Collectors.toList());
            setmealDishMapper.insertBatch(setmealDishes);
        }
        return row;
    }

    /**
     * 启售、停售套餐
     *
     * @param id
     * @param status
     * @return
     */
    public int startOrStop(Long id, Integer status) {
        // 判断是否包含停售的菜品
        if (status == StatusConstant.ENABLE) {
            List<Dish> list = dishMapper.getBySetmealId(id);
            if (list != null && list.size() != 0) {
                for (Dish dish : list) {
                    if (dish.getStatus().equals(StatusConstant.DISABLE)) {
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }

                }
            }
        }

        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        return setmealMapper.update(setmeal);
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     * @return
     */
    @Transactional
    public int deleteBatch(List<Long> ids) {
        // 判断菜品是否启售，启售则不能删除
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            Integer status = setmeal.getStatus();
            if (status == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        // 删除套餐
        int row = setmealMapper.deleteBatch(ids);
        // 删除套餐的菜品
        setmealDishMapper.deleteBatchBySetmeals(ids);
        return row;
    }

    /**
     * 查询套餐
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal){
        return setmealMapper.list(setmeal);
    }
}
